package com.hujiheng.paike.controller;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.server.HttpServerResponse;
import com.hujiheng.paike.common.lang.Result;
import com.hujiheng.paike.pojo.Admin;
import com.hujiheng.paike.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/admin")
public class LoginController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("/login")
    public Object login(@Validated @RequestBody Admin loginAdmin, HttpServerResponse response) {
        Admin user = adminService.getAdminByUsername(loginAdmin.getUsername());
//        Assert.notNull(user, "用户不存在");
        if (user == null) {
            return Result.fail("用户不存在");
        }
        if (!user.getPassword().equals(loginAdmin.getPassword())) {
            return Result.fail("密码不正确");
        }
        return Result.success(MapUtil.builder()
                .put("username", user.getUsername())
                .map()
        );
    }

    @RequestMapping("/logout")
    public Result logout() {
        return Result.success(null);
    }
}
